﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AugustVisual
{
    [ToolboxItem(false)]
    public class RowCell : ContentControl
    {

        static RowCell()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RowCell), new FrameworkPropertyMetadata(typeof(RowCell)));
        }
        AugustDataView augustDataView = null;
        RowLine rowLine = null;
        string mark = "NG";

        private RowLine RowLineInternal
        {
            get
            {
                if (rowLine == null)
                {
                    rowLine = (RowLine)FrameworkElementHelper.FindParent<RowLine>(this, 1, "Row_Line");
                }
                return rowLine;
            }
            set
            {
                rowLine = value;
            }
        }

        Grid focusGrid = null;
        private Grid RowFocusGrid
        {
            get
            {
                if (focusGrid == null)
                {
                    focusGrid = (Grid)FrameworkElementHelper.Findchild(RowLineInternal, "FocusRowLine");
                }
                return focusGrid;
            }
            set
            {
                focusGrid = value;
            }
        }

        private Grid markGrid;

        public Grid MarkGrid
        {
            get
            {
                if (markGrid == null)
                {
                    markGrid = (Grid)FrameworkElementHelper.Findchild(this, "MarkRowCell");
                }
                return markGrid;
            }
            set { markGrid = value; }
        }

        public ColumBase RelativeColum
        {
            get { return (ColumBase)GetValue(RelativeColumProperty); }
            set { SetValue(RelativeColumProperty, value); }
        }
        public static readonly DependencyProperty RelativeColumProperty =
            DependencyProperty.Register("RelativeColum", typeof(ColumBase), typeof(RowCell), new FrameworkPropertyMetadata(null));

        public RowCell()
        {
            this.Focusable = true;
            if (!FrameworkElementHelper.IsInDesignMode)
            {
                Loaded += RowCell_Loaded;
            }
        }

        //public override void OnTextChanged(object sender, DependencyPropertyChangedEventArgs e)
        //{
        //    base.OnTextChanged(sender, e);
        //    if (MarkGrid == null || RowLineInternal == null)
        //        return;
        //    if (e.NewValue.ToString() == mark)
        //    {
        //        MarkGrid.Background = Brushes.Red;
        //    }
        //    else
        //    {
        //        MarkGrid.Background = Brushes.Transparent;
        //    }
        //    int index = augustDataView.Items.IndexOf(this.DataContext);
        //    RowLineInternal.FreshBackColor(mark, index);
        //}


        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            if (MarkGrid == null || RowLineInternal == null)
                return;
            if (newContent.ToString() == mark)
            {
                MarkGrid.Background = Brushes.Red;
            }
            else
            {
                MarkGrid.Background = Brushes.Transparent;
            }
            int index = augustDataView.Items.IndexOf(this.DataContext);
            RowLineInternal.FreshBackColor(mark, index);
        }


        private void RowCell_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.Content == null) return;
            if (this.Content.ToString() == mark)
            {
                MarkGrid.Background = Brushes.Red;
            }
            int index = augustDataView.Items.IndexOf(this.DataContext);
            RowLineInternal.FreshBackColor(mark, index);
        }

        protected override void OnVisualParentChanged(DependencyObject oldParent)
        {
            base.OnVisualParentChanged(oldParent);
            augustDataView = FrameworkElementHelper.FindParent<AugustDataView>(this, 1, null);
            //Grid focusGridManager = FrameworkElementHelper.FindParent<Grid>(this, 1, "FocusManager");
            //lostFocusContentControl = FrameworkElementHelper.Findchild(focusGridManager, "LostFocusContent") as ContentControl;
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.Focus();
            augustDataView.FocusedObject = DataContext;
            augustDataView.FocusedColumn = RelativeColum;
        }

        //protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        //{
        //    base.OnMouseDoubleClick(e);
        //   // this.DataContext.GetType().GetProperty(RelativeColum.DisplayMemberBinding.Path.Path).SetValue(DataContext, "OK");
        //}


        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            RowFocusGrid.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
            //this.Foreground = Brushes.White;
        }


        protected override void OnLostFocus(RoutedEventArgs e)
        {
            base.OnLostFocus(e);
            RowFocusGrid.Background = Brushes.Transparent;
            //this.Foreground = rowCellInitForeColor;
        }
    }
}
